package com.lmh.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.lmh.common.Constants;


/**
 * @program bigdata-service
 * @Description 数据库连接池, 离线分析使用hive， 实时分析使用clickhouse
 * @Author limeihua
 * @Date 2024/08/14 20:04
 * @Version 1.0
 */
public class DataBasePoolUtil {
    private static final DruidDataSource clickhouseDataSource;
    private static final DruidDataSource hiveDataSource;

    static {
        clickhouseDataSource = new DruidDataSource();
        clickhouseDataSource.setUrl(Constants.CLICKHOUSE_URL);
        clickhouseDataSource.setUsername(Constants.CLICKHOUSE_USERNAME);
        clickhouseDataSource.setInitialSize(Constants.CLICKHOUSE_INITIALSIZE);
        clickhouseDataSource.setMinIdle(Constants.CLICKHOUSE_MINIDLE);
        clickhouseDataSource.setMaxActive(Constants.CLICKHOUSE_MAXACTIVE);
        clickhouseDataSource.setMaxWait(Constants.CLICKHOUSE_MAXWAIT);
        clickhouseDataSource.setTimeBetweenEvictionRunsMillis(Constants.CLICKHOUSE_TIMEBETWEENEVICTIONRUNSMILLIS);

        hiveDataSource = new DruidDataSource();
        hiveDataSource.setUrl(Constants.HIVE_URL);
        hiveDataSource.setUsername(Constants.HIVE_USERNAME);
        hiveDataSource.setInitialSize(Constants.HIVE_INITIALSIZE);
        hiveDataSource.setMinIdle(Constants.HIVE_MINIDLE);
        hiveDataSource.setMaxActive(Constants.HIVE_MAXACTIVE);
        hiveDataSource.setMaxWait(Constants.HIVE_MAXWAIT);
        hiveDataSource.setTimeBetweenEvictionRunsMillis(Constants.HIVE_TIMEBETWEENEVICTIONRUNSMILLIS);
    }
    public static DruidDataSource getDataSource(String dataBaseType){
        if("clickhouse".equals(dataBaseType)){
            return clickhouseDataSource;
        } else if ("hive".equals(dataBaseType)) {
            return hiveDataSource;
        }
        throw new IllegalArgumentException("数据库连接池还未配置此是数据库类型：【" + dataBaseType + "】");
    }

    private DataBasePoolUtil(){

    }
}
